package sk.datalan;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openrdf.repository.RepositoryException;

import sk.datalan.core.DatabaseManager;
import sk.datalan.similarity.ProcessManager;
import sk.datalan.sparqlreasoner.SparqlResoning;

/**
 * Servlet implementation class HelloWorld
 */
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HelloWorld() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
	}

	public String print() {
		startRepository(); //load file and run sparql multithread reasoning
		new ProcessManager().start(); //process all similarity rutines
		try {
			DatabaseManager.getInstance().getRepository().shutDown();
			DatabaseManager.getInstance().resetRepository();
		} catch ( RepositoryException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "Done!";
	}

	private void startRepository() {

		try {
			long startTime = System.currentTimeMillis();
			
			//load file
			DatabaseManager.getInstance().loadFile( new File( "C:\\Users\\Marek\\workspaceDatalan\\individual\\testdata\\week-03-2012.owl" ) );
			DatabaseManager.getInstance().flush();

			//multithread reasoning - query running
			new SparqlResoning( new File( "C:\\Users\\Marek\\AppData\\Roaming\\Aduna\\OpenRDF Sesame\\srules.sr" ) ).start();
			DatabaseManager.getInstance().flush();
		
			
			System.out.println( "Total time: " + ( System.currentTimeMillis() - startTime ) + "ms" );

		} catch ( RepositoryException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch ( IOException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
